package com.houxy.service.impl;

import com.houxy.repository.mapper.UserMapper;
import com.houxy.repository.model.entity.User;
import com.houxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Hou
 * @date 2018/11/1 11:42
 * @describe：
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User userLogin(User user) {
        return userMapper.findByUserNameAndPassWord(user.getUserName(), user.getPassword());
    }

    @Override
    public String registerUser(User user) {
        user.setIsDelete(0);
        User byUserName = userMapper.findByUserName(user.getUserName());
        if (byUserName != null) {
            return "用户名已存在！";
        }
        int insert = userMapper.insertSelective(user);
        if (insert > 0) {
            return "注册成功！";
        }
        return "注册失败！";
    }

    @Override
    public boolean updateUser(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(int userId) {
        User user = findUserById(userId);
        if (user != null) {
            user.setIsDelete(1);
        }
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }

    @Override
    public User findUserById(int userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
}
